
// do "/projects/hsieh_project/code_0_general/f_reg.do"

capture program drop f_reg_create
program f_reg_create
    args fr nvar
    
    capture noi frame drop `fr'
    frame create `fr'
    
    if `nvar' == 1 {
    frame `fr' {
        gen reg = ""
        gen b_x = .
        gen se_x = .
        gen p_x = .
        gen b_c = .
        gen se_c = .
        gen p_c = .
        gen r2 = .
        gen df_r = .
        gen n = .
    }
    }
end

capture program drop f_reg_append
program f_reg_append
    args fr x
    frame `fr' {
        local nobs = _N + 1
        set obs `nobs'
        replace reg = "`e(cmdline)'" if _n == `nobs'
        replace b_x = _b[`x'] if _n == `nobs'
        replace se_x = _se[`x'] if _n == `nobs'
        replace b_c = _b[_cons] if _n == `nobs'
        replace se_c = _se[_cons] if _n == `nobs'
        replace r2 = `e(r2)' if _n == `nobs'
        replace df_r = `e(df_r)' if _n == `nobs'
        replace n = `e(N)' if _n == `nobs'
        
        replace p_x = 2*ttail(df_r, abs(b_x/se_x))
        replace p_c = 2*ttail(df_r, abs(b_c/se_c))
    }
end

capture program drop f_reg_save
program f_reg_save
    args fr dsi

    frame `fr' {
        rounding_4dig "b_x se_x p_x b_c se_c p_c r2"
        save `dsi'_out, replace
        export delimited `dsi'_out.csv, replace   
    }
end

// End of do file
